import {openCamera,imageOperation} from 'assets/js/toonModule.js'
import Util from 'assets/js/util'
import $ from 'n-zepto';
var doubleSwitch=true;
function doubleClick(){
  if(doubleSwitch){
    doubleSwitch=false;
    setTimeout(function () { doubleSwitch = true }, 2100);
    return true;
  }else{
    return false;
  }
}
export default {
  data(){
    return {
      subjectData: {},
      shelveCount: 0,
      oldShelve: 0,
      showWarnModal:false, //格式校验提示框
      warnContent:''  //提示文案
    }
  },
  mounted(){
    this.init()
  },
  methods:{
    init: function(){
      var _this = this;
      var id = this.$route.query.subjectId;
      $(document.body).scrollTop(0);
      if(this.$route.query.shelveCount!="undefined") this.shelveCount = this.$route.query.shelveCount;
      var potion = {
        subjectId: id
      };
      this.$http.post('convSubjectManage/querySubjectDetail',potion)
        .then(function(resData){
          if(resData.meta.code == '0'){
            let data=resData.data;
            data.subjectName = data.subjectName .replace(/#/g , '')
            _this.subjectData = data;
            _this.oldShelve = data.isShelve;
          }else{
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: resData.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = resData.meta.message;
          }
        })
        .catch(function(err){
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })
    },
    checkname: function(eve){
      this.subjectData.subjectName=this.subjectData.subjectName.replace(/[\#]/g,'').replace(/[\＃]/g,'');
      // var index = this.subjectData.subjectName.indexOf('#');
      // if(index!=-1&&index){
      //   this.subjectData.subjectName=this.subjectData.subjectName.replace('#','');
      // }
    },
    updateSubject: function(){
      if(!doubleClick()){
        return;
      }
      var _this = this;
      var exp = /^\s*(.*?)\s*$/;
      var exps = /\s+/;
      this.subjectData.subjectName = this.subjectData.subjectName.replace(exp,"$1").replace(exps,'');
      // this.subjectData.detailContent = this.subjectData.detailContent.replace(exp,"$1").replace(exps,'');
      // this.subjectData.hint = this.subjectData.hint.replace(exp,"$1").replace(exps,'');
      //this.subjectData.detailContent = this.subjectData.detailContent.replace(/[\r\n]{3,}/g,'\r\n\r\n').replace(/[\r\n]*$/g,'').replace(/^[\r\n]*/g,'');
      //this.subjectData.hint = this.subjectData.hint.replace(/[\r\n]{3,}/g,'\r\n\r\n').replace(/[\r\n]*$/g,'').replace(/^[\r\n]*/g,'');
      var params = {
        subjectName: this.subjectData.subjectName,
        picUrl: this.subjectData.picUrl,
        detailContent: this.subjectData.detailContent,
        hint: this.subjectData.hint,
        isShelve: this.subjectData.isShelve==false ? 0: 1,
        subjectId: this.$route.query.subjectId
      };
      if(this.checkEditSubmit()){
        if(this.subjectData.isShelve==0){
          this.queryUpdateSubject(params);
        }else{
          if(this.shelveCount==5&&this.oldShelve==0){
            // this.$Modal.warning({
            //   title: '提示',
            //   content: "轮播展示最多有5个专题，请下架后再将此专题上架！"
            // });
            _this.showWarnModal = true;
            _this.warnContent = "轮播展示最多有5个专题，请下架后再将此专题上架！";
          }else{
              this.queryUpdateSubject(params);
          }
        }
      }
    },
    queryUpdateSubject: function(params){
        var _this = this;
      _this.$http.post('convSubjectManage/updateSubject',params)
        .then(function(resData){
          if(resData.meta.code == '0'){
            _this.$router.push({
              name: 'subjectList'
            });
          }else{
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: resData.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = resData.meta.message;
          }
        })
        .catch(function(err){
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: "保存失败，请重试！"
          // });
          _this.showWarnModal = true;
          _this.warnContent = "保存失败，请重试！";
        })
    },
    checkEditSubmit: function(){
      var i=0,
           errorStr='';
      if(!(this.subjectData.subjectName.length<=12&&this.subjectData.subjectName.length>=1)){
        i++;
        errorStr ="专题名称字数限制为1到12个字!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.subjectName)){
        i++;
        errorStr ="专题名称不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.picUrl==""){
        i++;
        errorStr ="请上传轮播图片!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.detailContent.replace(/[\r\n]/g,'**').length<200){
        i++;
        errorStr ="详情介绍需要二百个以上!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.detailContent)){
        i++;
        errorStr ="详情介绍不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(this.subjectData.hint.replace(/[\r\n]/g,'**').length<50){
        i++;
        errorStr ="提示文案需要五十字以上!<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(Util.isEmojiCharacter(this.subjectData.hint)){
        i++;
        errorStr ="提示文案不能包含emoji表情<br>"
        this.$Notice.warning({
          title:'温馨提示',
          desc:errorStr,
          duration:2
        })
        return false;
      }
      if(i==0){
        return true;
      }
    },
    outDialog: function(){
      var _this = this;
      openCamera(function (data){
        let params={
          src:'data:image/png;base64,'+data.base64,
          width:379
        };
        imageOperation(params,function (yasuohou_base64) {
          var index = yasuohou_base64.indexOf(',');
          let params={
            fileType:'.png',
            fileStream:yasuohou_base64.substring(index+1)
          };
          //请求
          _this.$http.post('convSubjectManage/uploadImg',params)
            .then(function(res){
              console.log("image"+res);
              _this.subjectData.picUrl = res.data.url;
            })
        });
      },null,{},200)

    },
    closeModal(){
      this.showWarnModal = false;
    }

  }
}
